package com.earnings.okhttputils.utils.ui.activity.user;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.databinding.ItemFeedbackBinding;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.adapter.DataBindingBaseadapter;
import com.earnings.okhttputils.utils.adapter.DataBindingViewHolder;
import com.earnings.okhttputils.utils.bean.MsgData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.utils.CommonUtil;
import com.earnings.okhttputils.utils.view.FullStaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
反馈
 */

public class UserFeedbcakActivity extends GodLeftHandBaseActivity implements View.OnClickListener {


    private RecyclerView recyclerView;
    private DataBindingBaseadapter dataBindingBaseadapter;
    Map<Integer,View> contMap;
    private EditText text;
    private int type;
    private TextView count;
    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_user_feedback;
    }

    @Override
    protected void init() {
        setTitle("反馈");
        recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
        text= (EditText) findViewById(R.id.text);
        count= (TextView) findViewById(R.id.count);
        findViewById(R.id.sumbit).setOnClickListener(this);
        initData();
        text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                count.setText(text.length()+"/100");
            }
        });
    }

    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.sumbit) {
            uploadFeedback();

        }
    }


    public void initData(){
        contMap=new HashMap<Integer, View>();
        String[] item=new String[]{	"反馈类型","软件功能","配送服务","商品问题","支付问题","    其他    "};
        final List<MsgData> datas=new ArrayList<>();
        for (int i = 0; i < item.length; i++) {
            MsgData home=new MsgData();
            home.setText(item[i]);
            datas.add(home);
        }
        recyclerView.setLayoutManager(new FullStaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        dataBindingBaseadapter = new DataBindingBaseadapter<MsgData, ItemFeedbackBinding>(UserFeedbcakActivity.this, datas, R.layout.item_feedback) {

            @Override
            public void convert(DataBindingViewHolder holder, ItemFeedbackBinding binding, MsgData homeButton) {


                binding.setMsg(homeButton);

                final int position=holder.getLayoutPosition();
                if (position==0){
                    holder.getView(R.id.text).setBackgroundResource(R.drawable.bg_style_round);
                    ((TextView)holder.getView(R.id.text)).setTextColor(getResources().getColor(R.color.white));
                       }
                if(contMap.get(position)==null)
                    contMap.put(position,holder.getView(R.id.view));
            }


            @Override
            public void onItemClick(DataBindingViewHolder holder, MsgData homeButton) {
                for (int i = 0; i <datas.size() ; i++) {
                    View view=  contMap.get(i);
                    ((TextView)contMap.get(i).findViewById(R.id.text)).setBackgroundResource(R.drawable.bg_white_round_style_line);
                    ((TextView)contMap.get(i).findViewById(R.id.text)).setTextColor(getResources().getColor(R.color.style));
                    //((TextView)contMap.get(i).findViewById(R.id.info)).setTextColor(mView.getResources().getColor(R.color.textmiddle));

                }
                View view=holder.getView(R.id.text);
                view.setBackgroundResource(R.drawable.bg_style_round);
                ((TextView)view).setTextColor(view.getResources().getColor(R.color.white));
                type=(holder.getLayoutPosition()+1);
                //((TextView)view.findViewById(R.id.info)).setTextColor(view.getResources().getColor(R.color.white));

            }
        };
        recyclerView.setAdapter(dataBindingBaseadapter);
    }

    /*
             提交
               */
    public void uploadFeedback() {
        Map map = new HttpMap(getContext());
        map.put( "uid" , CommonUtil.getUser().getUser_id());
        map.put( "type" ,type+"");
        map.put( "content" ,text.getText().toString());
        OkHttpUtils.post().url( HttpUrl.USER_FEEDBACK_URL).params( map ).build().execute( new HttpObjectCallback<String>(getContext() ) {
            @Override
            public void onSuccess(String object, String msg) {
                finish();
                ToastUtils.showToast( getContext(),msg );

            }

            @Override
            public void onFail(int code, String msg) {

                ToastUtils.showToast( getContext(),msg );
            }

        } );

    }
}
